package cn.org.xuanzp.examples.v1;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.Scanner;

public class NettyFtpClient {
	private String host;// 当前netty客户端连接的主机地址
	private int port;// 当前netty客户端连接的端口
	private Channel channel;
	private EventLoopGroup mainGroup;

	public NettyFtpClient(String host, int port) {
		this.host = host;
		this.port = port;
		this.mainGroup = new NioEventLoopGroup();
	}

	
	public Channel getChannel() {
		return channel;
	}

	// 启动当前netty服务器的操作
	public void start() throws Exception {
		Bootstrap b = new Bootstrap(); // (1)
		b.group(mainGroup); // (2)
		b.channel(NioSocketChannel.class); // (3)
		//b.option(ChannelOption.SO_KEEPALIVE, true); // (4)
		//NettyFtpClientHandler nettyClientHandler = new NettyFtpClientHandler();
		b.handler(new NettyFtpClientInitializer());
		/*
		b.handler(new ChannelInitializer<SocketChannel>() {
			@Override
			public void initChannel(SocketChannel ch) throws Exception {
				ch.pipeline().addLast(nettyClientHandler).addLast(new StringEncoder())// 添加传输数据的转码器
						.addLast(new StringDecoder());// 添加接收数据的解码器
			}
		});*/

		// Start the client.
		ChannelFuture future = b.connect(host, port).sync(); // (5)
		// 添加监听器，用于判断是否连接成功或者连接失败
		future.addListener(new ChannelFutureListener() {

			public void operationComplete(ChannelFuture future) throws Exception {
				if (future.isSuccess()) {
					System.out.println("连接服务器成功");

				} else {
					System.out.println("连接服务器失败");
					future.cause().printStackTrace();
					mainGroup.shutdownGracefully(); // 关闭线程组
				}

			}
		});
		this.channel = future.channel();

	}

	// 创建向服务器发送消息的方法
	public void send(String message) {
		Channel channel = this.getChannel();
		channel.writeAndFlush(message);
	}
	
	public void send(Object obj) {
		Channel channel = this.getChannel();
		channel.writeAndFlush(obj);
	}

	boolean isRun = true;

	// 客户端的被动关闭操作,由服务器发起操作
	public void close() {
		isRun = false;
		mainGroup.shutdownGracefully();// 关闭当前的线程组
	}

	// 客户端的主动关闭操作
	public void closeAndSendToServer() {
		send("CLIENT CLOSED");
		mainGroup.shutdownGracefully();// 关闭当前的线程组
	}

	public Command getUserOption() {
		System.out.println("请输入指令执行操作：");
		System.out.println("1.文件上传\n2.文件下载\n3.列出所有可下载的文件\n0.退出");
		Scanner clientInput = new Scanner(System.in);
		String nextLine = clientInput.nextLine();
		switch (nextLine) {
		case "1":			
			return new FileUploadCommand("uploadFile");
		case "2":
			return new FileDownloadCommand("downloadFile");
		case "3":
			return new FileListCommand();
		case "0":
			closeAndSendToServer();// 关闭当前的客户端
			return null;
		default:
			return null;
		}
	}

	// 当前客户端的操作
	public void clientOption() {
		while (isRun) {
			Command command = getUserOption();
			if (!this.getChannel().isActive() || !this.getChannel().isOpen()) {
				close();// 关闭当前的客户端
				break;
			}
			if(command!=null) {
				// 注意这个地方必须传递Object,否则出现不解析的问题
				send(command);
			}else {
				System.out.println("command is null.....");
			}
			
		}

	}

}

